/*
 * Copyright (c) 2024 Shenzhen Kaihong Digital Industry Development Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "sle_connect_callback_proxy.h"
#include "sle_hilog.h"

namespace OHOS {
namespace NearLink {
namespace SleStandard {
int32_t SleConnectCallbackProxy::InnerTransact(
    uint32_t code, MessageOption &flags, MessageParcel &data, MessageParcel &reply)
{
    SLE_LOGD("InnerTransact In");
    auto remote = Remote();
    if (remote == nullptr) {
        SLE_LOGE("get remote failed, error code %{public}d", code);
        return OBJECT_NULL;
    }
    int32_t err = remote->SendRequest(code, data, reply, flags);
    if (err != NO_ERROR) {
        SLE_LOGE("send request failed, ipcErr=%{public}d code %{public}d", err, code);
    }
    return err;
}

void SleConnectCallbackProxy::OnConnectStateChange(uint16_t connectId, const SleAddress &address, int32_t state, int32_t pairState, uint16_t reason)
{
    SLE_LOGD("OnConnectStateChange In");
    MessageParcel data;
    SLE_ASSERT_AND_RETURN_LOG(data.WriteInterfaceToken(SleConnectCallbackProxy::GetDescriptor()),
        "write interface token failed!");
    SLE_ASSERT_AND_RETURN_LOG(data.WriteUint16(connectId), "write connnect id failed!");
    SLE_ASSERT_AND_RETURN_LOG(data.WriteParcelable(&address), "write address failed!");
    SLE_ASSERT_AND_RETURN_LOG(data.WriteInt32(state), "write connect state failed!");
    SLE_ASSERT_AND_RETURN_LOG(data.WriteInt32(pairState), "write pair state failed!");
    SLE_ASSERT_AND_RETURN_LOG(data.WriteUint16(reason), "write reason failed!");

    MessageParcel reply;
    MessageOption option = {MessageOption::TF_ASYNC};
    int32_t result = InnerTransact(SleConnectCallbackInterfaceCode::SLE_CALLBACK_CONNECT_STATE, option, data, reply);
    if (result != NO_ERROR) {
        SLE_LOGE("transact done fail, error code: %{public}d", result);
        return;
    }
}

void SleConnectCallbackProxy::OnConnectParamUpdateComplete(uint16_t connectId, uint16_t interval, uint16_t latency, uint16_t timeout, uint32_t errCode)
{
    SLE_LOGD("OnConnectParamUpdateComplete In");
    MessageParcel data;
    SLE_ASSERT_AND_RETURN_LOG(data.WriteInterfaceToken(SleConnectCallbackProxy::GetDescriptor()),
        "write interface token failed!");
    SLE_ASSERT_AND_RETURN_LOG(data.WriteUint16(connectId), "write connnect id failed!");
    SLE_ASSERT_AND_RETURN_LOG(data.WriteUint16(interval), "write interval failed!");
    SLE_ASSERT_AND_RETURN_LOG(data.WriteUint16(latency), "write latency failed!");
    SLE_ASSERT_AND_RETURN_LOG(data.WriteUint16(timeout), "write timeout failed!");
    SLE_ASSERT_AND_RETURN_LOG(data.WriteUint32(errCode), "write result status failed!");

    MessageParcel reply;
    MessageOption option = {MessageOption::TF_ASYNC};
    int32_t result = InnerTransact(SleConnectCallbackInterfaceCode::SLE_CALLBACK_CONNECT_PARAM_UPDATE_COMPLETE, option, data, reply);
    if (result != NO_ERROR) {
        SLE_LOGE("transact done fail, error code: %{public}d", result);
        return;
    }
}

void SleConnectCallbackProxy::OnAuthComplete(uint16_t connectId, const SleAddress &address, const SleAuthenticationInfo &info, uint32_t errCode)
{
    SLE_LOGD("OnAuthComplete In");
    MessageParcel data;
    SLE_ASSERT_AND_RETURN_LOG(data.WriteInterfaceToken(SleConnectCallbackProxy::GetDescriptor()),
        "write interface token failed!");
    SLE_ASSERT_AND_RETURN_LOG(data.WriteUint16(connectId), "write connnect id failed!");
    SLE_ASSERT_AND_RETURN_LOG(data.WriteParcelable(&address), "write address failed!");
    SLE_ASSERT_AND_RETURN_LOG(data.WriteParcelable(&info), "write auth info failed!");
    SLE_ASSERT_AND_RETURN_LOG(data.WriteUint32(errCode), "write result status failed!");

    MessageParcel reply;
    MessageOption option = {MessageOption::TF_ASYNC};
    int32_t result = InnerTransact(SleConnectCallbackInterfaceCode::SLE_CALLBACK_AUTH_COMPLETE, option, data, reply);
    if (result != NO_ERROR) {
        SLE_LOGE("transact done fail, error code: %{public}d", result);
        return;
    }
}

void SleConnectCallbackProxy::OnPairComplete(uint16_t connectId, const SleAddress &address, uint32_t errCode)
{
    SLE_LOGD("OnAuthComplete In");
    MessageParcel data;
    SLE_ASSERT_AND_RETURN_LOG(data.WriteInterfaceToken(SleConnectCallbackProxy::GetDescriptor()),
        "write interface token failed!");
    SLE_ASSERT_AND_RETURN_LOG(data.WriteUint16(connectId), "write connnect id failed!");
    SLE_ASSERT_AND_RETURN_LOG(data.WriteParcelable(&address), "write address failed!");
    SLE_ASSERT_AND_RETURN_LOG(data.WriteUint32(errCode), "write result status failed!");

    MessageParcel reply;
    MessageOption option = {MessageOption::TF_ASYNC};
    int32_t result = InnerTransact(SleConnectCallbackInterfaceCode::SLE_CALLBACK_PAIR_COMPLETE, option, data, reply);
    if (result != NO_ERROR) {
        SLE_LOGE("transact done fail, error code: %{public}d", result);
        return;
    }
}

void SleConnectCallbackProxy::OnReadRssiComplete(uint16_t connectId, int8_t rssi, uint32_t errCode)
{
    SLE_LOGD("OnReadRssiComplete In");
    MessageParcel data;
    SLE_ASSERT_AND_RETURN_LOG(data.WriteInterfaceToken(SleConnectCallbackProxy::GetDescriptor()),
        "write interface token failed!");
    SLE_ASSERT_AND_RETURN_LOG(data.WriteUint16(connectId), "write connnect id failed!");
    SLE_ASSERT_AND_RETURN_LOG(data.WriteInt8(rssi), "write rssi failed!");
    SLE_ASSERT_AND_RETURN_LOG(data.WriteUint32(errCode), "write result status failed!");

    MessageParcel reply;
    MessageOption option = {MessageOption::TF_ASYNC};
    int32_t result = InnerTransact(SleConnectCallbackInterfaceCode::SLE_CALLBACK_READ_RSSI, option, data, reply);
    if (result != NO_ERROR) {
        SLE_LOGE("transact done fail, error code: %{public}d", result);
        return;
    }
}
} // SleStandard
} // NearLink
} // OHOS